package com.test.nnooolwdeaer.view_wooden.info_input_wooden;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.icu.util.Calendar;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.TextView;

import com.test.nnooolwdeaer.R;
import com.test.nnooolwdeaer.bean_wooden.process_wooden.ProcessData_wooden;
import com.test.nnooolwdeaer.util_wooden.StringUtil_wooden;
import com.test.nnooolwdeaer.util_wooden.http_wooden.HttpManager_wooden;
import com.test.nnooolwdeaer.view_wooden.base_wooden.BaseInfoInputViewManager_wooden;
import com.test.nnooolwdeaer.view_wooden.base_wooden.OnInputChangeListener_wooden;


public class DatePickViewManager_wooden extends BaseInfoInputViewManager_wooden {

    private TextView titleTv_wooden;
    private ViewGroup clickLayout_wooden;
    private TextView inputTv_wooden;

    private OnInputChangeListener_wooden onInputChangeListener_wooden;

    private long startTime;

    public DatePickViewManager_wooden(View view, ProcessData_wooden data,String trackWord){
        super(view,data,trackWord);
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void init_wooden(){

        titleTv_wooden = mView_wooden.findViewById(R.id.view_date_picker_tv_title);
        clickLayout_wooden = mView_wooden.findViewById(R.id.view_date_picker_layout_click);
        inputTv_wooden = mView_wooden.findViewById(R.id.view_date_picker_tv_input);

        setTitle_wooden(StringUtil_wooden.getSafeString_wooden(mData_wooden.getTitle_wooden()));
        inputTv_wooden.setHint(StringUtil_wooden.getSafeString_wooden(mData_wooden.getHint_wooden()));

        clickLayout_wooden.setOnClickListener(v -> {

            startTime = System.currentTimeMillis();

            AlertDialog.Builder builder = new AlertDialog.Builder(mView_wooden.getContext());
            View dialogView = ((Activity) mView_wooden.getContext()).getLayoutInflater().inflate(R.layout.dialog_date_picker_wooden,null);
            DatePicker datePicker = dialogView.findViewById(R.id.dialog_date_picker_view);
            datePicker.setCalendarViewShown(false);

            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.YEAR, -18);
            long maxDate = calendar.getTimeInMillis();
            datePicker.setMaxDate(maxDate);

            builder.setView(dialogView).setTitle(mView_wooden.getResources().getString(R.string.dialog_date_picker_title));
            builder.setPositiveButton(mView_wooden.getResources().getString(R.string.word_submit),(dialog, which) -> {
                int month = datePicker.getMonth() + 1;
                int day = datePicker.getDayOfMonth();
                String monthStr = month<10 ? "0"+month:String.valueOf(month);
                String dayStr = day<10 ? "0"+day:String.valueOf(day);
                //String input = dayStr+"/"+monthStr+"/"+datePicker.getYear();
                String input = datePicker.getYear()+"-"+monthStr+"-"+dayStr;

                long endTime = System.currentTimeMillis();
                HttpManager_wooden.getInstance_wooden().saveUserBuriedPoint(trackWord,input+"$$"+(endTime-startTime));

                inputTv_wooden.setText(input);
                if (onInputChangeListener_wooden !=null) onInputChangeListener_wooden.onInputChange_wooden(input);
                dialog.cancel();
            });
            builder.setNegativeButton(mView_wooden.getResources().getString(R.string.word_cancel), ((dialog, which) -> dialog.cancel()));
            builder.show();
        });

    }

    @Override
    public boolean checkInput_wooden() {
        if (!mData_wooden.isMustInput_wooden()) return true;
        return !TextUtils.isEmpty(inputTv_wooden.getText());
    }

    @Override
    public String getInput_wooden() {
        return StringUtil_wooden.getSafeString_wooden(inputTv_wooden.getText().toString());
    }

    public void setTitle_wooden(String title){
        if (TextUtils.isEmpty(title)) titleTv_wooden.setVisibility(View.GONE);
        else {
            titleTv_wooden.setVisibility(View.VISIBLE);
            titleTv_wooden.setText(title);
        }
    }

    public void setOnInputChangeListener_wooden(OnInputChangeListener_wooden onInputChangeListener_wooden) {
        this.onInputChangeListener_wooden = onInputChangeListener_wooden;
    }
}
